package com.iqiyi.pps.epg.web.utils.tag;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: wayne
 * Date: 12-6-5
 * Time: 上午11:23
 */
public class DisplayTag extends BodyTagSupport {

    private String entity;
    
    private String queryCol = "value";

    private String key = "id";

    private Object keyValue;

    private boolean dbQuery = true;

    private static final Map<String,Map<String,String>> memoryMap = new HashMap<String, Map<String, String>>();

    /**
     * 清除缓存map
     */
    public static void clearMemoryMap(){
        DisplayTag.memoryMap.clear();
    }


    @Override
    public int doStartTag() throws JspException {
        try {
            StringBuffer results = new StringBuffer("");
            pageContext.getOut().write(results.toString());
        } catch (IOException ex) {
            throw new JspTagException("doStartTag错误");
        }
        return EVAL_BODY_INCLUDE;
//return EVAL_BODY_BUFFERED;
    }

    @Override
    public int doEndTag() throws JspException {
        try {
            BodyContent bodyContent = this.getBodyContent();
            if(this.getKeyValue() == null || "".equals(this.getKeyValue())){
                if(bodyContent != null){
                    this.setKeyValue(bodyContent.getString());
                } else {
                    this.setKeyValue("");
                }
            }

            StringBuffer results = new StringBuffer("");

            results.append(showText());
            pageContext.getOut().write(results.toString());
        } catch (IOException ex) {
            throw new JspTagException("错误");
        }
        return EVAL_PAGE;
    }
//
//    @Override
//    public int doAfterBody() throws JspException {
//        if(this.getKeyValue() == null || "".equals(this.getKeyValue())){
//            if(this.getBodyContent() != null){
//                this.setKeyValue(this.getBodyContent().getString());
//            } else {
//                this.setKeyValue("");
//            }
////            this.setKeyValue(this.getBodyContent() == null ? "":this.getBodyContent().getString());
//        }
//        return EVAL_PAGE;
//    }

    /**
     * 根据参数返回显示文本
     * @return
     */
    private String showText(){
        String text = "";

//        getRealKeyValue();
//        Map<String,String> map= memoryMap.get(""+queryCol+"_"+entity+"_"+key);
        if(memoryMap.containsKey("" + queryCol + "_" + entity + "_" + key)){
            Map<String,String> map= memoryMap.get(""+queryCol+"_"+entity+"_"+key);
            if(map.containsKey(keyValue+"")){
               text = map.get(keyValue+"");
                return text;
            }
        }else {
            memoryMap.put(""+queryCol+"_"+entity+"_"+key,new HashMap<String, String>());
        }

        return text;
    }

    private  void getRealKeyValue(){
//          if("java.lang.Long".equals(keyValueType.trim())){
//              keyValue = Long.valueOf((String)keyValue);
//          }else if("java.lang.Integer".equals(keyValueType.trim())){
//              keyValue = Integer.valueOf((String)keyValue);
//          }
    }

    public String getEntity() {
        return entity;
    }

    public void setEntity(String entity) {
        this.entity = entity;
    }

    public String getQueryCol() {
        return queryCol;
    }

    public void setQueryCol(String queryCol) {
        this.queryCol = queryCol;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Object getKeyValue() {
        return keyValue;
    }

    public void setKeyValue(Object keyValue) {
        this.keyValue = keyValue;
    }

//    public String getKeyValueType() {
//        return keyValueType;
//    }
//
//    public void setKeyValueType(String keyValueType) {
//        this.keyValueType = keyValueType;
//    }

    public boolean isDbQuery() {
        return dbQuery;
    }

    public void setDbQuery(boolean dbQuery) {
        this.dbQuery = dbQuery;
    }
}
